/**
 * 
 */
package org.gigi.security.web;

import org.gigi.core.Page;
import org.gigi.core.web.BasicResponse;
import org.gigi.core.web.BeanResponse;
import org.gigi.core.web.GridResponse;
import org.gigi.security.domain.User;
import org.gigi.security.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * UserController
 *
 * @author Eugene Wang 2011-3-28
 * @see
 */
@Controller
@RequestMapping("/sec/user")
public class UserController {
	/*Logger*/
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserService userService;
	/**
	 * 新创建一个用户
	 * @param user
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public @ResponseBody BeanResponse<User> create(@RequestBody User user) {
		if (logger.isDebugEnabled()) {
			logger.debug("创建用户>" + user.toString());
		}
		userService.create(user);
		BeanResponse<User> response = new BeanResponse<User>("成功创建用户", user);
		return response;
	}

	/**
	 * 修改一个用户
	 * @param username
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public @ResponseBody BeanResponse<User> update(@RequestBody User user) {
		if (logger.isDebugEnabled()) {
			logger.debug("修改用户>{username="+user.getUsername()+"} "+user);
		}
		userService.update(user);
		BeanResponse<User> response = new BeanResponse<User>("成功修改用户", user);
		return response;
	}
	/**
	 * 获取用户列表（分页）
	 * @param page
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public @ResponseBody GridResponse<User> getPage(Page<User> page) {
		if (logger.isDebugEnabled()) {
			logger.debug("获取分页>"+page.toString());
		}
		Page<User> resultPage = userService.getPage(page);
		return new GridResponse<User>(resultPage);
	}

	/**
	 * 读取一个用户
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "/{username}",  method=RequestMethod.GET)
	public @ResponseBody BeanResponse<User> get(@PathVariable("username") String username) {
		if (logger.isDebugEnabled()) {
			logger.debug("获取用户>{username="+username+"} ");
		}
		User user = userService.findByUsername(username);
		BeanResponse<User> response = new BeanResponse<User>();
		response.setData(user);
		return response;
	}

	/**
	 * 删除用户
	 * @param username 用户名
	 */
	@RequestMapping(value="/{username}", method=RequestMethod.DELETE)
	public @ResponseBody BasicResponse remove(@PathVariable("username") String username) {
		if (logger.isDebugEnabled()) {
			logger.debug("删除用户>{username = " + username + "}");
		}
		userService.delete(username);
		return new BasicResponse("成功删除用户");
	}
}
